package psrc.logic.pages.login;

import java.util.Map;

import psrc.logic.util.HashSHA256;
import psrc.logic.util.beans.UsuarioBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginController extends ActionSupport  {

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;

	public String execute() throws Exception {
		
		UsuarioBean usuarioBean = new UsuarioBean();
		usuarioBean.setUsuario(username);
		String pass;
		try{
			pass = HashSHA256.encode(password);
		} catch (Exception e) {
			return ERROR;
		}
		usuarioBean.setSenha(pass);
		try{
			if (LoginModel.verifyLogin(usuarioBean)) {
				Map session = ActionContext.getContext().getSession();
				session.put("logined", "true");
				session.put("username", username);
				return SUCCESS;
			} else {
				addActionError(getText("login.error"));
				return ERROR;
			}			
		} catch (Exception e) {
			return "genericError";
		}
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

}
